package com.kafka.java访问kafka实例;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class ProducerSample {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.147.3:9092,192.168.147.4:9092,192.168.147.5:9092");
        props.put("key.serializer",
                "org.apache.kafka.common.serialization.IntegerSerializer");
        props.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("zk.connect","192.168.147.3:2181,192.168.147.4:2181,192.168.147.5:2181");

        String topic = "testTopic";
        Producer<String,String> producer = new KafkaProducer<String, String>(props);

        producer.send(new ProducerRecord<String, String>(topic,"java-message1 中国"));
        producer.send(new ProducerRecord<String, String>(topic,"java-message2 中国"));
        producer.send(new ProducerRecord<String, String>(topic,"java-message3 中国"));

        producer.close();
    }
}
